package com.yuelao.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/9/12 16:13
 * @修改记录 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
@Slf4j
public class FileUtil {
    public static boolean write(String path, String fileName, String text) {
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            String fullPath = path + File.separator + fileName;
            File folder = new File(path);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            fos = new FileOutputStream(fullPath);
            osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            bw.write(text);
            bw.flush();
            log.info("写入" + fileName + "成功");
            return true;
        } catch (IOException e) {
            log.error("写入 {} error : {}", fileName, e.getCause());
            try {
                fos.close();
                osw.close();
                bw.close();
            } catch (IOException e1) {
                log.error("close error : {}", e1.getCause());
            }
        }
        return false;
    }

    public static List<String> readerUser() {
        String filePath = "";
        List<String> list = null;
        try {
            FileInputStream fis = new FileInputStream(filePath + "user.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            String content = "";
            try {
                String info = null;
                while ((info = br.readLine()) != null) {
                    content += info;
                }
            } catch (Exception e) {
            }
            String[] arr = content.split(" ");
            list = new ArrayList();
            for (String unit : arr) {
                list.add(unit.trim());
            }
            log.info(list.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

}
